package com.actions.bluetoothbox2.util;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.actions.bluetoothbox2.R;
import com.actions.bluetoothbox2.app.BrowserActivity;

/**
 * Created by liuyong on 2017/12/25.
 */

public class PopupVoiceSelecter extends PopupWindow implements View.OnClickListener {

    private boolean[] isCheck;
    private onSelecteListener selected;
    private PopupVoiceSelecter2 popupVoiceSelecter;
    private PopVoiceSelectAdapter adapter;
    private int pos = 0;
    private int imgID = R.drawable.ic_color1;
    private View pop_view1;
    private BrowserActivity activity;

    public PopupVoiceSelecter(BrowserActivity context, PopupVoiceSelecter2 popupVoiceSelecter2, View pop_view) {
        super(context);
        View view = LayoutInflater.from(context).inflate(R.layout.pop_voiceselect1_layout, null);
        this.popupVoiceSelecter = popupVoiceSelecter2;
        this.pop_view1 = pop_view;
        this.activity = context;

        GridView pop_grid_voice = (GridView) view.findViewById(R.id.pop_grid_voice);
        Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
        Button btn_ok = (Button) view.findViewById(R.id.btn_ok);

        isCheck = new boolean[]{true, false, false, false, false, false, false, false, false, false, false, false, false, false, false};
        adapter = new PopVoiceSelectAdapter(context, isCheck);
        pop_grid_voice.setAdapter(adapter);

        pop_grid_voice.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 14) {
                    if (activity.is_TF) {
                        popupVoiceSelecter.showAtLocation(pop_view1, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                        dismiss();
                    } else {
                        Toast.makeText(activity, "卡不存在，请选择内置铃声", Toast.LENGTH_SHORT).show();
                    }
                    return;
                }
                for (int i = 0; i < isCheck.length; i++) {
                    isCheck[i] = false;
                }
                isCheck[position] = true;
                pos = position;
                imgID = adapter.imgID[position];
                adapter.notifyDataSetChanged();

            }
        });

        btn_cancel.setOnClickListener(this);
        btn_ok.setOnClickListener(this);

        setContentView(view);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.mypopwindow_anim_style);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
    }

    public void setSelected(onSelecteListener selected) {
        this.selected = selected;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel:
                this.dismiss();
                break;
            case R.id.btn_ok:
                this.dismiss();
                if (null != selected) {
                    selected.onSelected(pos, imgID);
                }
                break;
            default:
                break;
        }
    }

    public interface onSelecteListener {
        void onSelected(int position, int imgID);
    }

}
